package com.le.tester.handler.global;

import com.le.tester.common.ServerResponse;
import com.le.tester.common.enums.ResponseCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * createTime：2022/11/9 9:53
 * description：全局异常处理
 */
//注解解释该类为异常处理类，默认返回json格式
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    //捕获Exception异常
    @ExceptionHandler(Exception.class)
    public ServerResponse<Exception> exceptionHandler(Exception e) {
        LOG.error("catch exception,message {}", e.getMessage());
        return ServerResponse.createByErrorCodeMsgData(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc(), e);
    }
}
